home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / devel / vbcc-wos-src / vlink / dir.c < prev    next >
C/C++ Source or Header  |  1999-01-01  |  3KB  |  145 lines

  1. /* $VER: vlink dir.c V0.5d (22.08.98)
  2.  *
  3.  * This file is part of vlink, a portable linker for multiple
  4.  * object formats.
  5.  * Copyright (c) 1997-99  Frank Wille
  6.  *
  7.  * vlink is freeware and part of the portable and retargetable ANSI C
  8.  * compiler vbcc, copyright (c) 1995-99 by Volker Barthelmann.
  9.  * vlink may be freely redistributed as long as no modifications are
  10.  * made and nothing is charged for it. Non-commercial usage is allowed
  11.  * without any restrictions.
  12.  * EVERY PRODUCT OR PROGRAM DERIVED DIRECTLY FROM MY SOURCE MAY NOT BE
  13.  * SOLD COMMERCIALLY WITHOUT PERMISSION FROM THE AUTHOR.
  14.  *
  15.  *
  16.  * v0.5d (22.08.98) phx
  17.  *       New function chk_file(), to check for the presence of a file.
  18.  * v0.2  (28.02.98) phx
  19.  *       Replaced AllocMem() for AMIGAOS by malloc(), to avoid
  20.  *       problems with PowerPC.
  21.  * v0.1  (27.02.98) phx
  22.  *       First version that seems to link AmigaOS ADOS and EHF
  23.  *       objects and libraries. Many common features, like linking
  24.  *       sections together which have relative references, are
  25.  *       still missing. Also, PowerPC-ELF32 support is about to come.
  26.  * v0.0  (02.12.97) phx
  27.  *       File created.
  28.  */
  29.  
  30.  
  31. #define DIR_C
  32. #include "vlink.h"
  33.  
  34. #ifdef AMIGAOS
  35. #include <dos/dosextens.h>
  36. #include <dos/dostags.h>
  37. #include <proto/dos.h>
  38.  
  39. struct Dir {
  40.   struct FileInfoBlock fib;
  41.   BPTR lock;
  42.   char name[FNAMEBUFSIZE];
  43. };
  44.  
  45. #else
  46. #include <dirent.h>
  47. #endif
  48.  
  49.  
  50. char *open_dir(char *);
  51. char *read_dir(char *);
  52. void close_dir(char *);
  53. bool chk_file(char *);
  54.  
  55.  
  56.  
  57. #ifdef AMIGAOS
  58.  
  59. char *open_dir(char *dirname)
  60. /* open a directory for examination */
  61. {
  62.   struct Dir *d;
  63.  
  64.   if (d = malloc(sizeof(struct Dir))) {
  65.     strcpy(d->name,dirname);
  66.     if (!strcmp(d->name,".")) {  /* current directory? */
  67.       d->name[0] = 0;
  68.     }
  69.     if (d->lock = Lock(d->name,ACCESS_READ)) {
  70.       if (Examine(d->lock,&(d->fib))) {
  71.         return ((char *)d);
  72.       }
  73.       UnLock(d->lock);
  74.     }
  75.     free(d);
  76.   }
  77.   return (NULL);
  78. }
  79.  
  80.  
  81. char *read_dir(char *d)
  82. /* get next file name from opened directory, NULL if no more entries */
  83. {
  84.   if (ExNext(((struct Dir *)d)->lock,&(((struct Dir *)d)->fib)))
  85.     return (((struct Dir *)d)->fib.fib_FileName);
  86.   if (IoErr() != ERROR_NO_MORE_ENTRIES)
  87.     error(10,((struct Dir *)d)->name);
  88.   return (NULL);
  89. }
  90.  
  91.  
  92. void close_dir(char *d)
  93. /* finish directory access */
  94. {
  95.   if (d) {
  96.     UnLock(((struct Dir *)d)->lock);
  97.     free(d);
  98.   }
  99. }
  100.  
  101.  
  102. #else /* UNIX */
  103.  
  104. char *open_dir(char *dirname)
  105. /* open a directory for examination */
  106. {
  107.   return ((char *)opendir(dirname));
  108. }
  109.  
  110.  
  111. char *read_dir(char *d)
  112. /* get next file name from opened directory, NULL if no more entries */
  113. {
  114.   struct dirent *dp;
  115.  
  116.   if (dp = readdir((DIR *)d))
  117.     return (dp->d_name);
  118.   return (NULL);
  119. }
  120.  
  121.  
  122. void close_dir(char *d)
  123. /* finish directory access */
  124. {
  125.   if (d)
  126.     closedir((DIR *)d);
  127. }
  128.  
  129. #endif
  130.  
  131.  
  132. bool chk_file(char *path)
  133. /* Check if file is present */
  134. {
  135.   FILE *f;
  136.  
  137. #ifdef AMIGAOS
  138.   if (!strncmp(path,"./",2))
  139.     path += 2;
  140. #endif
  141.   if (f = fopen(path,"r"))
  142.     fclose(f);
  143.   return (f != NULL);
  144. }
  145.